﻿namespace PhotobucketNet
{
    using System.Collections.Generic;
    using System.Xml;

    public class MediaItemList : List<MediaItem>
    {
        internal static MediaItemList CreateFromXmlDocument(XmlDocument document)
        {
            return CreateFromXmlNodeList(document.SelectNodes("descendant::media"));
        }

        internal static MediaItemList CreateFromXmlNodeList(XmlNodeList mediaNodeList)
        {
            MediaItemList list = new MediaItemList();
            foreach (XmlNode node in mediaNodeList)
            {
                list.Add(MediaItem.CreateFromXmlNode(node));
            }
            return list;
        }

        internal static MediaItemList CreateFromXmlResponseMessage(XmlResponseMessage responseMessage)
        {
            return CreateFromXmlDocument(responseMessage.ResponseXml);
        }

        public MediaItem Find(MediaItem mediaItem)
        {
            foreach (MediaItem item in this)
            {
                if (item.Name == mediaItem.Name)
                {
                    return item;
                }
            }
            throw new PhotobucketApiException(ErrorCode.CouldNotGet, "Could not find in mediaList");
        }
    }
}

